var DataTypes = require("sequelize").DataTypes;
var _class_ = require("./class");
var _course = require("./course");
var _course_homework = require("./course_homework");
var _course_type = require("./course_type");
var _course_video = require("./course_video");
var _dept = require("./dept");
var _dept_adm = require("./dept_adm");
var _ent = require("./ent");
var _ent_adm = require("./ent_adm");
var _ent_tea = require("./ent_tea");
var _file = require("./file");
var _log = require("./log");
var _notices = require("./notices");
var _permis = require("./permis");
var _region = require("./region");
var _role = require("./role");
var _role_permis = require("./role_permis");
var _stu = require("./stu");
var _univ = require("./univ");
var _univ_adm = require("./univ_adm");
var _univ_tea = require("./univ_tea");
var _user = require("./user");

function initModels(sequelize) {
  var class_ = _class_(sequelize, DataTypes);
  var course = _course(sequelize, DataTypes);
  var course_homework = _course_homework(sequelize, DataTypes);
  var course_type = _course_type(sequelize, DataTypes);
  var course_video = _course_video(sequelize, DataTypes);
  var dept = _dept(sequelize, DataTypes);
  var dept_adm = _dept_adm(sequelize, DataTypes);
  var ent = _ent(sequelize, DataTypes);
  var ent_adm = _ent_adm(sequelize, DataTypes);
  var ent_tea = _ent_tea(sequelize, DataTypes);
  var file = _file(sequelize, DataTypes);
  var log = _log(sequelize, DataTypes);
  var notices = _notices(sequelize, DataTypes);
  var permis = _permis(sequelize, DataTypes);
  var region = _region(sequelize, DataTypes);
  var role = _role(sequelize, DataTypes);
  var role_permis = _role_permis(sequelize, DataTypes);
  var stu = _stu(sequelize, DataTypes);
  var univ = _univ(sequelize, DataTypes);
  var univ_adm = _univ_adm(sequelize, DataTypes);
  var univ_tea = _univ_tea(sequelize, DataTypes);
  var user = _user(sequelize, DataTypes);

  dept.belongsToMany(user, { as: 'userid_users', through: dept_adm, foreignKey: "deptid", otherKey: "userid" });
  ent.belongsToMany(user, { as: 'userid_user_ent_adms', through: ent_adm, foreignKey: "entid", otherKey: "userid" });
  permis.belongsToMany(role, { as: 'role_id_roles', through: role_permis, foreignKey: "permis_id", otherKey: "role_id" });
  role.belongsToMany(permis, { as: 'permis_id_permis', through: role_permis, foreignKey: "role_id", otherKey: "permis_id" });
  univ.belongsToMany(user, { as: 'userid_user_univ_adms', through: univ_adm, foreignKey: "univid", otherKey: "userid" });
  user.belongsToMany(dept, { as: 'deptid_depts', through: dept_adm, foreignKey: "userid", otherKey: "deptid" });
  user.belongsToMany(ent, { as: 'entid_ents', through: ent_adm, foreignKey: "userid", otherKey: "entid" });
  user.belongsToMany(univ, { as: 'univid_univs', through: univ_adm, foreignKey: "userid", otherKey: "univid" });
  course_homework.belongsTo(course, { as: "course", foreignKey: "course_id"});
  course.hasMany(course_homework, { as: "course_homeworks", foreignKey: "course_id"});
  course_video.belongsTo(course, { as: "course", foreignKey: "course_id"});
  course.hasMany(course_video, { as: "course_videos", foreignKey: "course_id"});
  course.belongsTo(course_type, { as: "type_course_type", foreignKey: "type"});
  course_type.hasMany(course, { as: "courses", foreignKey: "type"});
  class.belongsTo(dept, { as: "dept", foreignKey: "deptid"});
  dept.hasMany(class, { as: "classes", foreignKey: "deptid"});
  dept_adm.belongsTo(dept, { as: "dept", foreignKey: "deptid"});
  dept.hasMany(dept_adm, { as: "dept_adms", foreignKey: "deptid"});
  ent_adm.belongsTo(ent, { as: "ent", foreignKey: "entid"});
  ent.hasMany(ent_adm, { as: "ent_adms", foreignKey: "entid"});
  role_permis.belongsTo(permis, { as: "permi", foreignKey: "permis_id"});
  permis.hasMany(role_permis, { as: "role_permis", foreignKey: "permis_id"});
  ent.belongsTo(region, { as: "city_region", foreignKey: "city"});
  region.hasMany(ent, { as: "ents", foreignKey: "city"});
  univ.belongsTo(region, { as: "city", foreignKey: "city_id"});
  region.hasMany(univ, { as: "univs", foreignKey: "city_id"});
  role_permis.belongsTo(role, { as: "role", foreignKey: "role_id"});
  role.hasMany(role_permis, { as: "role_permis", foreignKey: "role_id"});
  user.belongsTo(role, { as: "role_role", foreignKey: "role"});
  role.hasMany(user, { as: "users", foreignKey: "role"});
  dept.belongsTo(univ, { as: "univ", foreignKey: "univid"});
  univ.hasMany(dept, { as: "depts", foreignKey: "univid"});
  univ_adm.belongsTo(univ, { as: "univ", foreignKey: "univid"});
  univ.hasMany(univ_adm, { as: "univ_adms", foreignKey: "univid"});
  dept_adm.belongsTo(user, { as: "user", foreignKey: "userid"});
  user.hasMany(dept_adm, { as: "dept_adms", foreignKey: "userid"});
  ent_adm.belongsTo(user, { as: "user", foreignKey: "userid"});
  user.hasMany(ent_adm, { as: "ent_adms", foreignKey: "userid"});
  ent_tea.belongsTo(user, { as: "tea", foreignKey: "teaid"});
  user.hasOne(ent_tea, { as: "ent_tea", foreignKey: "teaid"});
  file.belongsTo(user, { as: "creator_user", foreignKey: "creator"});
  user.hasMany(file, { as: "files", foreignKey: "creator"});
  log.belongsTo(user, { as: "user", foreignKey: "user_id"});
  user.hasMany(log, { as: "logs", foreignKey: "user_id"});
  stu.belongsTo(user, { as: "stu", foreignKey: "stuid"});
  user.hasOne(stu, { as: "stu", foreignKey: "stuid"});
  univ_adm.belongsTo(user, { as: "user", foreignKey: "userid"});
  user.hasMany(univ_adm, { as: "univ_adms", foreignKey: "userid"});
  univ_tea.belongsTo(user, { as: "tea", foreignKey: "teaid"});
  user.hasOne(univ_tea, { as: "univ_tea", foreignKey: "teaid"});

  return {
    class_,
    course,
    course_homework,
    course_type,
    course_video,
    dept,
    dept_adm,
    ent,
    ent_adm,
    ent_tea,
    file,
    log,
    notices,
    permis,
    region,
    role,
    role_permis,
    stu,
    univ,
    univ_adm,
    univ_tea,
    user,
  };
}
module.exports = initModels;
module.exports.initModels = initModels;
module.exports.default = initModels;
